﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using cMES1001.Services;
using Newtonsoft.Json;
using xClient.Framework;
using xClient.Framework.StaticResource;

namespace cMES1001
{
    public partial class EditBomMain : Form
    {
        private ServiceMethod serM = new ServiceMethod();

        public EditBomMain(string command, params object[] values)
        {
            InitializeComponent();
            cmbPartMode.Items.Clear();
            ExecutionResult<dynamic> codeList = ServiceMethod.GetPartCodeList();
            if (codeList.Status)
            {
                if (codeList.Data.Count>0)
                {
                    for (int i = 0; i < codeList.Data.Count; i++)
                    {
                        if (!cmbPartMode.Items.Contains(codeList.Data[i]["part_code"].ToString()))
                        {
                            cmbPartMode.Items.Add(codeList.Data[i]["part_code"].ToString());
                        }
                    }
                    cmbPartMode.SelectedIndex = 0;
                }
            }


            if (command.Equals("ADD"))
            {
                this.Text = "新增Bom料号信息";
                this.txtBomNo.Text = values[0].ToString();
                this.txtBomNo.Enabled = false;
                this.txtQty.Text = "";
                this.txtPartNo.Text = "";
                this.txtUnit.Text = "";
            }
            if (command.Equals("EDIT"))
            {
                this.Text = "修改Bom中材料信息";
                this.txtBomNo.Enabled = false;
                this.txtBomNo.Text = values[0].ToString();
                this.txtPartNo.Text = values[1].ToString();
                
                this.txtBomVersion.Text = values[3].ToString();
                this.txtQty.Text = values[4].ToString();
                this.txtUnit.Text = values[5].ToString();
                this.txtWastageRate.Text = values[6].ToString();
                this.txtAlternative.Text = values[7].ToString();
                this.txtSequence.Text = values[8].ToString();
                this.txtOperaton.Text = values[9].ToString();
                this.txtDesignatorLocation.Text = values[10].ToString();
                this.txtRemarks.Text = values[11].ToString();
                this.txtIsValid.Text = values[12].ToString();
                this.cmbPartMode.Text = values[2].ToString();  ///
            }

            btnEdit.Click += delegate
            {
                if (command.Equals("ADD"))
                {
                    if (txtBomNo.Text.Length < 1) { return; }
                    if (txtQty.Text.Length < 1) { return; }
                    if (txtAlternative.Text.Length < 1) { return; }
                    if (txtBomVersion.Text.Length < 1) { return; }
                    if (txtPartNo.Text.Length < 1) { return; }
                    if (txtDesignatorLocation.Text.Length < 1) { return; }
                    if (txtIsValid.Text.Length < 1) { return; }
                    if (txtOperaton.Text.Length < 1) { return; }
                    if (txtRemarks.Text.Length < 1) { return; }
                    if (txtSequence.Text.Length < 1) { return; }
                    if (txtUnit.Text.Length < 1) { return; }
                    if (txtWastageRate.Text.Length < 1) { return; }
                    if (cmbPartMode.Text.Length < 1) { return; }

                    string vm = JsonConvert.SerializeObject(new
                    {
                        vBomNo = txtBomNo.Text,
                        vQty = txtQty.Text,
                        vAlternative = txtAlternative.Text,
                        vBomVersion = txtBomVersion.Text,
                        vPartNo = txtPartNo.Text,
                        vDesignatorLocation = txtDesignatorLocation.Text,
                        vIsValid = txtIsValid.Text,
                        vOperaton = txtOperaton.Text,
                        vRemarks = txtRemarks.Text,
                        vSequence = txtSequence.Text,
                        vUnit = txtUnit.Text,
                        vWastageRate = txtWastageRate.Text,
                        cby = StaticDictionary.Dictionary.UserID,
                        mby = StaticDictionary.Dictionary.UserID,
                        vPartCode=cmbPartMode.Text
                    });

                    ExecutionResult result = ServiceMethod.AddBomPartNo(vm);

                    if (result.Status)
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.No;
                        MessageBox.Show(result.Message, "系统消息");
                    }
                }

                if (command.Equals("EDIT"))
                {
                    if (txtBomNo.Text.Length < 1) { return; }
                    if (txtQty.Text.Length < 1) { return; }
                    if (txtPartNo.Text.Length < 1) { return; }
                    if (txtUnit.Text.Length < 1) { return; }

                    string vm = JsonConvert.SerializeObject(new
                    {
                        vBomNo = txtBomNo.Text,
                        vQty = txtQty.Text,
                        vAlternative = txtAlternative.Text,
                        vBomVersion = txtBomVersion.Text,
                        vPartNo = txtPartNo.Text,
                        vDesignatorLocation = txtDesignatorLocation.Text,
                        vIsValid = txtIsValid.Text,
                        vOperaton = txtOperaton.Text,
                        vRemarks = txtRemarks.Text,
                        vSequence = txtSequence.Text,
                        vUnit = txtUnit.Text,
                        vWastageRate = txtWastageRate.Text,
                        cby = StaticDictionary.Dictionary.UserID,
                        mby = StaticDictionary.Dictionary.UserID,
                        vPartCode = cmbPartMode.Text
                    });

                    ExecutionResult result = ServiceMethod.UpdateBomByPartNo(vm);

                    if (result.Status)
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.No;
                        MessageBox.Show(result.Message, "系统消息");
                    }
                }
            };
        }
    }
}
